package com.akuhome.resource.service.entity;

import com.akuhome.resource.dao.CharacterDao;
import com.akuhome.resource.entity.Character;
import com.akuhome.resource.enums.ResultEnum;
import com.akuhome.resource.exception.ResourceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CharacterService {
    @Autowired
    private CharacterDao characterDao;

    public List<Character> findAllCharacter(){
        return characterDao.findAllCharacter();
    }

    public Character findCharacterById(Integer characterId){
        return characterDao.findCharacterById(characterId);
    }

    public Integer insertCharacter(Character character){
        Integer row = characterDao.insertCharacter(character);
        if(row == 1){
            return character.getId();
        }else{
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
    }

    public Object updateCharacter(Character character){
        Integer row = characterDao.updateCharacter(character);
        if(row == 1){
            return null;
        }else if(row == 0){
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }else{
            throw new ResourceException(ResultEnum.UNKNOWN_ERROR);
        }
    }

    public Object deleteCharacterById(Integer characterId){
        Integer row = characterDao.deleteCharacterById(characterId);
        if(row == 1){
            return null;
        }else{
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
    }

    public void enableAuthByCharacterId(Integer characterId,Long authCode){
        Integer row = characterDao.enableAuthByCharacterId(characterId,authCode);
        if(row != 1){
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
    }

    public void disableAuthByCharacterId(Integer characterId,Long authCode){
        Integer row = characterDao.disableAuthByCharacterId(characterId,authCode);
        if(row != 1){
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
    }

    public void updateAuthByCharacterId(Integer characterId,Long authCode){
        Integer row = characterDao.updateAuthByCharacterId(characterId,authCode);
        if(row != 1){
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
    }

    public void addCharacterUserCount(Integer characterId,Integer count){
        Integer row = characterDao.addCharacterUserCount(characterId,count);
        if(row != 1){
            throw new ResourceException(ResultEnum.ARG_MISMATCH_ERROR);
        }
    }
}
